package com.uniqueyi.baselib.utilsbase;

import android.app.ActivityManager;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.InputType;
import android.util.Log;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by
 * author:  cwy.
 * date:    2017/12/26.  9:22.
 * prj:     crxls.
 */


public class Utils {
    private static Context context;

    public static void initUitls(Context ctx) {
        context=ctx;
    }

    public static Context getCtx() {
        return context;
    }

    /**
     * 密码显示或隐藏 （切换）
     */
    public static boolean showOrHide(EditText etPassword) {
        boolean isShow;
        //记住光标开始的位置
        int pos = etPassword.getSelectionStart();
        if (etPassword.getInputType() != (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)) {//隐藏密码
            etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            isShow = false;
        } else {//显示密码
            etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            isShow = true;
        }
        etPassword.setSelection(pos);
        return isShow;
    }

    /**
     * 验证手机号码
     * <p>
     * 移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147
     * 联通号码段:130、131、132、136、185、186、145
     * 电信号码段:133、153、180、189
     *
     * @param cellphone
     * @return
     */
    public static boolean checkCellphone(String cellphone) {
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
        Pattern pattern = Pattern.compile(regex);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(cellphone);
        // 字符串是否与正则表达式相匹配
        boolean rs = matcher.matches();
        return rs;
    }

    public static long getDateMulit() {
        return System.currentTimeMillis();
    }

    public static String getDeviceId(Context context) {
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return tm.getDeviceId();
        } catch (Exception e) {
            try {
                String SerialNumber = android.os.Build.SERIAL;
                return SerialNumber;
            } catch (Exception e1) {
                return "";
            }
        }
    }


    /**
     * 当前服务是否存在
     *
     * @param context
     * @param className "com.ipearl.glasses.services.BLeScanService"
     * @return
     */
    public static boolean isWorked(Context context, String className) {
        Log.d("MainActivity", "isWorked: " + System.currentTimeMillis());
        ActivityManager myManager = (ActivityManager) context
                .getApplicationContext().getSystemService(
                        Context.ACTIVITY_SERVICE);
        ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager
                .getRunningServices(Integer.MAX_VALUE);
        if (runningService.size() <= 0) {
            return false;
        }
        for (int i = 0; i < runningService.size(); i++) {
            Log.d("MainActivity", "isWorked: " + i + "==" + runningService.get(i).service.getClassName().toString());
            if (runningService.get(i).service.getClassName().toString()
                    .equals(className)) {
                Log.d("MainActivity", "true isWorked: " + System.currentTimeMillis());
                return true;
            }
        }
        Log.d("MainActivity", "false isWorked: " + System.currentTimeMillis());
        return false;
    }


    public static int ScreenHeight = 0;
    public static int ScreenWidth = 0;
}
